#!/usr/bin/python
# -*- coding: utf-8 -*-
# @name             : 浏览器驱动配置
# @author           : OneYoung
# @create           : 2024/02/04
# @version          : v1.0
# @desc             : 封装常用的浏览器驱动

from selenium import webdriver
from application.settings import BROWSER_NAME, HEADLESS


def browser():
    """ 定义浏览器驱动 """
    try:
        browser_name = BROWSER_NAME.title()
        # 是否启用无头模式
        if HEADLESS:
            # 根据浏览器名称动态加载浏览器驱动
            options = getattr(webdriver, f'{browser_name}Options')()
            options.add_argument('--headless')
            driver = getattr(webdriver, browser_name)(options=options)
        else:
            driver = getattr(webdriver, browser_name)()
        return driver
    except Exception as ec:
        print(f'浏览器信息有误,请检查.{ec}')
        raise EnvironmentError('浏览器信息有误,请检查.')
